package edu.zqu.collegeField.project.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.School;
import edu.zqu.collegeField.project.mapper.SchoolMapper;
import edu.zqu.collegeField.project.service.SchoolService;
import edu.zqu.collegeField.project.vo.SchoolVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 校区 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService {
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;

    @Override
    public List<SchoolVo> get(BigInteger collegeId, //获取校区列表
                              String name, String englishName) {
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("englishName", englishName);
        map.put("name", name);
        List<SchoolVo> schoolVoList = BeanMapper.mapList(schoolMapper.get(map), SchoolVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (SchoolVo schoolVo : schoolVoList) {
            schoolVo.setCollegeName(collegeName);
        }
        return schoolVoList;
    }

    @Override
    public SchoolVo getById(BigInteger id) {//获取校区
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        SchoolVo schoolVo = BeanMapper.map(schoolMapper.getById(map), SchoolVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(schoolVo.getCollegeId());
        schoolVo.setCollegeName(collegeName);
        return schoolVo;
    }

    @Override
    public String getNameById(BigInteger id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        return schoolMapper.getNameById(map);
    }

    @Override
    public SchoolVo getByName(BigInteger collegeId,String name) {//获取校区
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("name", StrUtil.trimToNull(name));
        SchoolVo schoolVo = BeanMapper.map(schoolMapper.getByName(map), SchoolVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(schoolVo.getCollegeId());
        schoolVo.setCollegeName(collegeName);
        return schoolVo;
    }

    @Override
    public int add(School school) {//添加校区
        school.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return schoolMapper.insert(school);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(School schoolUpdate) {//修改校区
        School school = schoolMapper.selectById(schoolUpdate.getId());
        if (StringUtils.isNull(school)) {
            throw new NullPointerException("找不到该校区");
        }
        BeanMapper.copyPropertiesNotNull(schoolUpdate, school);
        try {
            int insert = schoolMapper.insert(school.setLastVersion(schoolUpdate.getId()));
            if (insert >= 1) {
                return delete(schoolUpdate.getId(), schoolUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除校区
        try {
            UpdateWrapper<School> SchoolUpdateWrapper = new UpdateWrapper<School>().eq("id", Convert.toStr(id));
            int update = schoolMapper.update(new School().setByWho(byWho), SchoolUpdateWrapper);
            if (update >= 1) {
                return schoolMapper.delete(SchoolUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
